package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Order;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;


    /**
     * data:{
     * setmealNames:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎'],
     * setmealCounts:[
     * {value:335, name:'直接访问'},
     * {value:310, name:'邮件营销'},
     * {value:234, name:'联盟广告'},
     * {value:135, name:'视频广告'},
     * {value:154, name:'搜索引擎'}
     * ]
     * }
     */
    @Override
    public Map getSetmealReport() {
        List<Map> setmealCounts = setmealMapper.findEachSetmealCountByName();
        List<String> setmealNames = new ArrayList<>();

        Map<String, Object> map = new HashMap<>(2);

        for (Map setmeal : setmealCounts) {
            setmealNames.add((String) setmeal.get("name"));
        }
        map.put("setmealCounts",setmealCounts);
        map.put("setmealNames", setmealNames);

        return map;
    }

    /**
     * "data":{
     * "reportDate":"2019-04-25",  今日日期
     *
     * "todayVisitsNumber":0,  今日到诊数
     * "todayNewMember":0,  今日新增会员数
     * "todayOrderNumber":0,  今日预约数
     *
     * "thisWeekVisitsNumber":0,  本周到诊数
     * "thisWeekNewMember":0,  本周新增会员数
     * "thisWeekOrderNumber":0,  本周预约数
     *
     * "thisMonthNewMember":2,  本月新增会员数
     * "thisMonthOrderNumber":2,  本月预约数
     * "thisMonthVisitsNumber":0,  本月到诊数
     *
     * "totalMember":10,  总会员数
     *
     * "hotSetmeal":
     *  [{"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5},
     *   {"proportion":0.1818,"name":"阳光爸妈升级肿瘤12项筛查体检套餐","setmeal_count":2},
     *   {"proportion":0.1818,"name":"珍爱高端升级肿瘤12项筛查","setmeal_count":2},
     *   {"proportion":0.0909,"name":"孕前检查套餐","setmeal_count":1}  ],},
     *
     * @return
     */
    @Override
    public Map getBusinessReport() throws Exception {
        Map<String,Object> map = new HashMap<>(12);
        //获取今日日期
        String today =  DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周一
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月第一天
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //获取今日到诊数
        Integer todayVisitsNumber = orderMapper.findCountByDateIntervalAndStatus(today,today, Order.ORDERSTATUS_YES);
        //获取今日新增会员数
        Integer todayNewMember = memberMapper.findMemberCountByDateInterval(today,today);
        //获取今日预约数
        Integer todayOrderNumber = orderMapper.findCountByDateIntervalAndStatus(today, today,null);

        //获取本周到诊数
        Integer thisWeekVisitsNumber = orderMapper.findCountByDateIntervalAndStatus(thisWeekMonday, today, Order.ORDERSTATUS_YES);
        //获取本周会员新增
        Integer thisWeekNewMember = memberMapper.findMemberCountByDateInterval(thisWeekMonday, today);
        //获取本周预约数
        Integer thisWeekOrderNumber = orderMapper.findCountByDateIntervalAndStatus(thisWeekMonday, today,null);


        //获取本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.findCountByDateIntervalAndStatus(firstDay4ThisMonth, today, Order.ORDERSTATUS_YES);
        //获取本月会员新增
        Integer thisMonthNewMember = memberMapper.findMemberCountByDateInterval(firstDay4ThisMonth, today);
        //获取本月预约数
        Integer thisMonthOrderNumber = orderMapper.findCountByDateIntervalAndStatus(firstDay4ThisMonth, today,null);

        //获取总会员数
        Integer totalMember = memberMapper.findTotalCount();

        //获取热门套餐以及其比例
        List<Map> hotSetmeal = setmealMapper.findHotSetmeal();

        map.put("reportDate",today);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }
}
